
from fastapi import FastAPI, HTTPException

from models.collaborator import Collaborator
from util.log import Log
from util.redis import Redis
from common.const import BaseConstant

'''
通过 userId 获取协作者列表
'''
async def getListByUserId(userId: int):
    try:
        res = await Collaborator.filter(user_id=userId).all()
        return res
    except Exception as e:
        raise e

'''
通过 repositoryId、userId 判断是否为协作者
'''
async def isCollaborator(repositoryId: int, userId: int):
    try:
        res = await Collaborator.filter(repository_id=repositoryId, user_id=userId).first()
        Log.debug('res', res)
        if res == None:
            return False
        else:
            return True
    except Exception as e:
        raise e

'''
通过 repositoryId 获取 协作者 列表
'''
async def getListByRepositoryId(repositoryId: int):
    try:
        res = await Collaborator.filter(repository_id=repositoryId).all()
        return res
    except Exception as e:
        raise e

'''
添加协作者
'''
async def createCollaborator(collaborator: dict):
    try:
        res = await Collaborator.create(**collaborator)
        return res
    except Exception as e:
        raise e

'''
删除协作者
'''
async def deleteCollaborator(collaborator: dict):
    try:
        res = await Collaborator.filter(**collaborator).delete()
        return res
    except Exception as e:
        raise e

